home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / sortdemo.zip / BUBBLE.PAS < prev    next >
Pascal/Delphi Source File  |  1987-09-03  |  2KB  |  101 lines

  1.                                             { K.L. Noell, fhw 03.Sep.87 }
  2.  PROGRAM BubbleSort_Demo (output);
  3.  
  4.  CONST n = 639;                   { number of columns :  x-coordinates }
  5.        range = 199;               { actual values :      y-coordinates }
  6.  VAR
  7.       i1: INTEGER;
  8.       num,loops,swaps,aloops,aswaps: REAL;
  9.       A: ARRAY [0..n] OF INTEGER;
  10.  
  11.  
  12.  PROCEDURE BubbleSort ( np: INTEGER );
  13.  VAR
  14.      i,ie,temp: INTEGER;
  15.      flag: BOOLEAN;
  16.  
  17.  BEGIN
  18.      ie := np;
  19.  
  20.      REPEAT
  21.         flag := FALSE;
  22.         ie := ie - 1;
  23.         FOR i:= 0 TO ie DO BEGIN
  24.             loops := loops + 1;
  25.             IF A[i] > A[i+1] THEN     { swap }
  26.                BEGIN
  27.                flag := TRUE;
  28.                swaps := swaps + 1;
  29.                Plot (i,A[i],0);
  30.                Plot ((i+1),A[i+1],0);
  31.                temp := A[i];
  32.                A[i] := A[i+1];
  33.                A[i+1]:= temp;
  34.                Plot(i, A[i] ,3);
  35.                Plot((i+1),A[i+1],3);
  36.             END;
  37.         END;
  38.      UNTIL flag = FALSE;
  39.  
  40.  END;  { BubbleSort }
  41.  
  42.  
  43. { ----------------------------------------- }
  44.  
  45.  Begin  { Mainprogram for BubbleSort_Demo }
  46.  
  47.     HiRes;
  48.     HiResColor (Magenta);
  49.  
  50.     FOR i1:=1 TO n DO BEGIN
  51.        num := range*RANDOM;
  52.        A [i1] := TRUNC (num);
  53.        Plot (i1,A[i1],15);
  54.     END;
  55.  
  56.    GraphBackground (Magenta);
  57.    Palette (2);
  58.  
  59.   {Sorting start:}
  60.   loops := 0;
  61.   swaps := 0;
  62.   DELAY (1000);
  63.  
  64.      BubbleSort (n);
  65.  
  66.      aloops := loops;
  67.      aswaps := swaps;
  68.      writeln ('   Bubble Sort a)  Loops,Swaps: ',loops,swaps);
  69.      writeln;
  70.      writeln ('b) Press any key to process with an array already sorted,');
  71.      writeln ('   but in opposite direction.');
  72.  
  73.      REPEAT UNTIL KeyPressed;
  74.  
  75.   Hires;
  76.   GraphBackground(6);
  77.   Palette(2);
  78.     FOR i1:=1 TO n DO BEGIN
  79.        num := (n-i1)/(n/range);
  80.        A[i1] := TRUNC (num);
  81.        Plot (i1,A[i1],15);
  82.     END;
  83.  
  84.   loops := 0;
  85.   swaps := 0;
  86.   DELAY (1000);
  87.  
  88.   BubbleSort (n);
  89.  
  90.   REPEAT UNTIL KeyPressed;
  91.  
  92.      writeln (' Bubble Sort a)  Loops,Swaps: ',aloops,aswaps);
  93.      writeln (' Bubble Sort b)  Loops,Swaps: ',loops,swaps);
  94.      writeln;
  95.      writeln (' Press any key to exit.');
  96.  
  97.      REPEAT UNTIL KeyPressed;
  98.      TextMode;
  99.  
  100. end. { BubbleSort_Demo }
  101.